// file: main.go

package main

import (
	// "fmt"
	"jyhome/datasource"
	"jyhome/repositories"
	"jyhome/services"
	"jyhome/web/controllers"
	"jyhome/web/middleware"

	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	// "github.com/kataras/iris/websocket"
)

func main() {
	app := iris.New()
	app.Logger().SetLevel("debug")

	// Load the template files.
	app.RegisterView(iris.HTML("./web/views", ".html").Layout("layout.html"))

	// Serve our controllers.
	mvc.New(app.Party("/")).Handle(new(controllers.IndexController))
	mvc.New(app.Party("/Index")).Handle(new(controllers.IndexController))
	mvc.New(app.Party("/index")).Handle(new(controllers.IndexController))
	mvc.New(app.Party("/About")).Handle(new(controllers.AboutController))
	mvc.New(app.Party("/about")).Handle(new(controllers.AboutController))
	mvc.New(app.Party("/Contact")).Handle(new(controllers.ContactController))
	mvc.New(app.Party("/contact")).Handle(new(controllers.ContactController))
	mvc.New(app.Party("/Games")).Handle(new(controllers.GamesController))
	mvc.New(app.Party("/games")).Handle(new(controllers.GamesController))
	mvc.New(app.Party("/Engines")).Handle(new(controllers.EnginesController))
	mvc.New(app.Party("/engines")).Handle(new(controllers.EnginesController))
	mvc.New(app.Party("/Softs")).Handle(new(controllers.SoftsController))
	mvc.New(app.Party("/softs")).Handle(new(controllers.SoftsController))
	mvc.New(app.Party("/News")).Handle(new(controllers.NewsController))
	mvc.New(app.Party("/news")).Handle(new(controllers.NewsController))
	mvc.New(app.Party("/Single")).Handle(new(controllers.SingleController))
	mvc.New(app.Party("/single")).Handle(new(controllers.SingleController))

	app.Get("/chat", func(ctx iris.Context) {
		ctx.ServeFile("websockets.html", false) // second parameter: enable gzip?
	})

	// You can also split the code you write to configure an mvc.Application
	// using the `mvc.Configure` method, as shown below.

	//static path
	app.HandleDir("/js", "./static/js") // serve our custom javascript code
	app.HandleDir("/images", "./static/images") // serve our custom javascript code
	app.HandleDir("/fonts", "./static/fonts") // serve our custom javascript code
	app.HandleDir("/css", "./static/css") // serve our custom javascript code


	// setupWebsocket(app)

	// http://0.0.0.0:80/hello
	// http://0.0.0.0:80/hello/iris
	// http://0.0.0.0:80/movies
	// http://0.0.0.0:80/movies/1
	app.Run(
		// Start the web server at 0.0.0.0:80
		iris.Addr("0.0.0.0:80"),
		// skip err server closed when CTRL/CMD+C pressed:
		iris.WithoutServerError(iris.ErrServerClosed),
		// enables faster json serialization and more:
		iris.WithOptimizations,
	)
}

// note the mvc.Application, it's not iris.Application.
func movies(app *mvc.Application) {
	// Add the basic authentication(admin:password) middleware
	// for the /movies based requests.
	app.Router.Use(middleware.BasicAuth)

	// Create our movie repository with some (memory) data from the datasource.
	repo := repositories.NewMovieRepository(datasource.Movies)
	// Create our movie service, we will bind it to the movie app's dependencies.
	movieService := services.NewMovieService(repo)
	app.Register(movieService)

	// serve our movies controller.
	// Note that you can serve more than one controller
	// you can also create child mvc apps using the `movies.Party(relativePath)` or `movies.Clone(app.Party(...))`
	// if you want.
	app.Handle(new(controllers.MovieController))
}

// func setupWebsocket(app *iris.Application) {
// 	// create our echo websocket server
// 	ws := websocket.New(websocket.Config{
// 		// These are low-level optionally fields,
// 		// user/client can't see those values.
// 		ReadBufferSize:  1024,
// 		WriteBufferSize: 1024,
// 		// only javascript client-side code has the same rule,
// 		// which you serve using the ws.ClientSource (see below).
// 		EvtMessagePrefix: []byte("my-custom-prefix:"),
// 	})
// 	ws.OnConnection(handleConnection)

// 	// register the server on an endpoint.
// 	// see the inline javascript code in the websockets.html, this endpoint is used to connect to the server.
// 	app.Get("/echo", ws.Handler())

// 	// serve the javascript built'n client-side library,
// 	// see websockets.html script tags, this path is used.
// 	app.Any("/iris-ws.js", func(ctx iris.Context) {
// 		ctx.Write(ws.ClientSource)
// 	})
// }

// func handleConnection(c websocket.Connection) {
// 	// Read events from browser
// 	c.On("chat", func(msg string) {
// 		// Print the message to the console, c.Context() is the iris's http context.
// 		fmt.Printf("%s sent: %s\n", c.Context().RemoteAddr(), msg)
// 		// Write message back to the client message owner with:
// 		// c.Emit("chat", msg)
// 		// Write message to all except this client with:
// 		c.To(websocket.Broadcast).Emit("chat", msg)
// 	})
// }
